package com.anyou.mall.scancode;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;

/**
 * 扫码工具类
 */
@Slf4j
public class ScanCodeUtil {

    /**
     * 图片base64字符串的前缀
     */
    private static String base64Url = "data:image/png;base64,";

    public static void main(String[] args) {
        System.out.println("开始生成...");
        //根据字符串生成二维码图片的base64字符串
        String qrCode = createQRCode("www.baidu.com");
        System.out.println("qrCode:"+qrCode);
    }

//    /**
//     * url 跳转的网页地址
//     *　path　二维码图片的存储目录
//     * 返回图片的数据库路径　
//     * @author 18316
//     *
//     */
//    public static String codeByParms(String url,String path) {
//        String imgUrl="";
//        try {
//            path=path+"/qrcode";
//            File file = new File(path);
//            if(!file.exists()){
//                file.mkdirs();
//            }
//            String codeName = UUID.randomUUID().toString();// 二维码的图片名
//            String imageType = "jpg";// 图片类型
//            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
//            Map<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
//            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//            BitMatrix bitMatrix = multiFormatWriter.encode(url, BarcodeFormat.QR_CODE, 400, 400, hints);
//            File file1 = new File(path, codeName + "." + imageType);
//            MatrixToImageWriter.writeToFile(bitMatrix, imageType, file1);
//             imgUrl="/qrcode"+"/"+codeName+ "."+imageType;
//
//        } catch (WriterException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return imgUrl;
//
//    }
//    public static void code() {
//        try {
//            String content = "https://www.baidu.com";
//            String path = "F:/测试";// 二维码保存的路径
//            File file = new File(path);
//            if(!file.exists()){
//                file.mkdirs();
//            }
//            String codeName = UUID.randomUUID().toString();// 二维码的图片名
//            String imageType = "jpg";// 图片类型
//            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
//            Map<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
//            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//            BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400, hints);
//            File file1 = new File(path, codeName + "." + imageType);
//            MatrixToImageWriter.writeToFile(bitMatrix, imageType, file1);
//        } catch (WriterException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * 根据字符串创建二维码流生成图片的base64字符串
     * @param url 字符串
     * @return
     * @throws IOException
     * @throws WriterException
     */
    public static String createQRCode(String url)  {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        HashMap<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = null;
        String text="";
        try {
            bitMatrix = qrCodeWriter.encode(url, BarcodeFormat.QR_CODE, 600, 600, hints);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            MatrixToImageWriter.writeToStream(bitMatrix, "PNG", outputStream);
            Base64.Encoder encoder = Base64.getEncoder();
             text = encoder.encodeToString(outputStream.toByteArray());

        } catch (WriterException e) {
            e.printStackTrace();
            log.info("文件写出出错");
        } catch (IOException e) {
            e.printStackTrace();
            log.info("文件IO异常");
        }



        return base64Url + text;
    }
}
